package com.sunwayworld.basemodule.rm.train.examtmpl.validator;

import java.util.List;
import java.util.stream.Collectors;

import com.sunwayworld.basemodule.rm.train.exam.bean.LimsCoreExamBean;
import com.sunwayworld.basemodule.rm.train.exam.service.LimsCoreExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.validator.data.DataValidator;

@Component
public class LimsCoreExamTmplDeactivatableValidator implements DataValidator {
    @Autowired
    private LimsCoreExamService limsCoreExamService;

    public boolean doValid(Object... args) {
        RestJsonWrapperBean warpper = (RestJsonWrapperBean) args[0];
        List<Long> examTmplIdList = warpper.parseId(Long.class);
        
        List<LimsCoreExamBean> examList = limsCoreExamService.selectListByFilter(SearchFilter.instance().match("EXAMTMPLID", examTmplIdList).filter(MatchPattern.OR));
        
        boolean flag = true;
        
        if (!examList.isEmpty()) {
            
            List<String> tmplNameList = examList.stream().map(e -> e.getExt$Item("tmplname")).distinct().collect(Collectors.toList());
            
            tmplNameList.forEach(e -> {
                addConstraintViolation( e + I18nHelper.getMessage("CORE.MODULE.TRAIN.EXAM_TMPL.TIP.CANNOT_DEACTIVATE"));
            });
            flag = false;
        }
        return flag;
    }
}
